<template>
  <el-dropdown trigger="click" @command="handleCommand">
    <span class="el-dropdown-link">
      <svg-icon class-name="size-icon" icon-class="earth" />
    </span>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item
        v-for="(lang, i) in langs"
        :key="`Lang${i}`"
        :disabled="language === lang"
        :command="lang"
      >
        {{ langDict[lang] }}
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
export default {
  name: 'LocaleChanger',
  data() {
    return {
      langs: ['zh_CN', 'ru_RU', 'en_US'],
      langDict: {
        zh_CN: '简体中文',
        ru_RU: '俄罗斯语',
        en_US: '美式英语'
      }
    }
  },
  computed: {
    language() {
      return this.$store.getters.language || 'ru_RU'
    }
  },
  methods: {
    handleCommand(command) {
      this.$i18n.locale = command || 'ru_RU'
      this.$store.dispatch('app/setLanguage', command || 'ru_RU')
    }
  }
}
</script>
